//
//  XMMineSettingViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMMineSettingViewController.h"
#import "XMMineSettingLine.h"
#import "XMAccountSafeViewController.h"
#import "XMBlackListViewController.h"
#import "XMFeedbackListController.h"
#import "XMAboutViewController.h"
@interface XMMineSettingViewController ()
@property (nonatomic, strong) XMMineSettingLine *accountV;
@property (nonatomic, strong) XMMineSettingLine *messageV;
@property (nonatomic, strong) XMMineSettingLine *blackV;

@property (nonatomic, strong) XMMineSettingLine *updateV;
@property (nonatomic, strong) XMMineSettingLine *cacheV;
@property (nonatomic, strong) XMMineSettingLine *feedbackV;
@property (nonatomic, strong) XMMineSettingLine *helpV;
@property (nonatomic, strong) XMMineSettingLine *aboutV;

@property (nonatomic, strong) UIButton *logoutBtn;

@end

static CGFloat const kLineH = 50.0;

@implementation XMMineSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    self.navView.backgroundColor = kHEX(@"#F2F2F2");
    [self.navLeft2Btn setTitle:@"设置" forState:UIControlStateNormal];
    UIView *view1 = [[UIView alloc] init];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(17);
        make.right.equalTo(self.view).offset(-17);
        make.top.equalTo(self.navView.mas_bottom).offset(10);
        make.height.mas_equalTo(kLineH * 3);
    }];
    
    UIView *view2 = [[UIView alloc] init];
    [self.view addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(17);
        make.right.equalTo(self.view).offset(-17);
        make.top.equalTo(view1.mas_bottom).offset(10);
        make.height.mas_equalTo(kLineH * 5);
    }];
    
    view1.backgroundColor = UIColor.whiteColor;
    view2.backgroundColor = UIColor.whiteColor;

    kCornerRadius(view1, 10, 0, UIColor.whiteColor);
    kCornerRadius(view2, 10, 0, UIColor.whiteColor);
    
    XMWeakSelf(self);
    _accountV = [XMMineSettingLine viewFromXib];
    _accountV.titleLa.text = @"账号与安全";
    _accountV.showSwitch = NO;
    _accountV.clickBlock = ^{
        XMStrongSelf(self);
        XMAccountSafeViewController *vc = [[XMAccountSafeViewController alloc] initWithNibName:kStringFromClass(XMAccountSafeViewController) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    _messageV = [XMMineSettingLine viewFromXib];
    _messageV.titleLa.text = @"消息通知";
    _messageV.showSwitch = YES;
    _messageV.switchBlock = ^(BOOL isOn) {
        
    };
    
    _blackV = [XMMineSettingLine viewFromXib];
    _blackV.titleLa.text = @"黑名单";
    _blackV.showSwitch = NO;
    _blackV.line.hidden = YES;
    _blackV.clickBlock = ^{
        XMStrongSelf(self);
        XMBlackListViewController *vc = [[XMBlackListViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    _updateV = [XMMineSettingLine viewFromXib];
    _updateV.titleLa.text = @"软件更新";
    _updateV.showSwitch = NO;
    _updateV.descLa.text = [XMUtil bundleShortVersionString];
    _updateV.clickBlock = ^{
        
    };
    
    _cacheV = [XMMineSettingLine viewFromXib];
    _cacheV.titleLa.text = @"清除缓存";
    _cacheV.showSwitch = NO;
    _cacheV.descLa.text = [self getCurrentImgCache];

    _cacheV.clickBlock = ^{
        XMStrongSelf(self);
        [self clearCache];
    };
    
    _feedbackV = [XMMineSettingLine viewFromXib];
    _feedbackV.titleLa.text = @"意见反馈";
    _feedbackV.showSwitch = NO;
    _feedbackV.clickBlock = ^{
        XMStrongSelf(self);
        XMFeedbackListController *vc = [[XMFeedbackListController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    _helpV = [XMMineSettingLine viewFromXib];
    _helpV.titleLa.text = @"帮助中心";
    _helpV.showSwitch = NO;
    _helpV.clickBlock = ^{
        XMStrongSelf(self);
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"帮助中心";
        vc.urlStr = kGlobalVariables.appConfig.question;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    _aboutV = [XMMineSettingLine viewFromXib];
    _aboutV.titleLa.text = @"关于寻觅";
    _aboutV.showSwitch = NO;
    _aboutV.line.hidden = YES;
    _aboutV.clickBlock = ^{
        XMStrongSelf(self);
        XMAboutViewController *vc = [[XMAboutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    [view1 addSubview:_accountV];
    [view1 addSubview:_messageV];
    [view1 addSubview:_blackV];

    [view2 addSubview:_updateV];
    [view2 addSubview:_cacheV];
    [view2 addSubview:_feedbackV];
    [view2 addSubview:_helpV];
    [view2 addSubview:_aboutV];

    [_accountV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(view1);
        make.height.mas_equalTo(kLineH);
    }];

    [_messageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view1);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_accountV.mas_bottom);
    }];
    
    [_blackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view1);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_messageV.mas_bottom);
    }];
    
    
    [_updateV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(view2);
        make.height.mas_equalTo(kLineH);
    }];

    [_cacheV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view2);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_updateV.mas_bottom);
    }];
    
    [_feedbackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view2);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_cacheV.mas_bottom);
    }];
    
    [_helpV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view2);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_feedbackV.mas_bottom);
    }];
    
    [_aboutV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view2);
        make.height.mas_equalTo(kLineH);
        make.top.equalTo(_helpV.mas_bottom);
    }];
    
    _logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_logoutBtn setBackgroundColor:kAppMainColor];
    [_logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    kCornerRadius(_logoutBtn, 21, 0, UIColor.clearColor);
    [_logoutBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_logoutBtn];
    [_logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.top.equalTo(view2.mas_bottom).offset(24);
        make.height.mas_equalTo(42);
    }];
}

- (void)logout {
    [kNotificationCenter postNotificationName:@"xm_logout" object:nil];
}

- (NSString *)getCurrentImgCache {
    double bytesCache = @([[SDImageCache sharedImageCache] totalDiskSize]).doubleValue;
    NSString *clearCacheName;
    if (bytesCache >= 1000 * 1000 * 1000) {
        clearCacheName = kStringFormat(@"%.2fG", bytesCache / (1000 * 1000 * 1000));
    } else if (bytesCache >= 1000 * 1000) {
        clearCacheName = kStringFormat(@"%.2fMB", bytesCache / (1000 * 1000));
    } else {
        clearCacheName = kStringFormat(@"%.2fKB", bytesCache / (1000));
    }
    return clearCacheName;
}

- (void)clearCache {
    [XMAlertHelper alertTitle:@"确定清除本地缓存?"
                      message:nil
                  cancelTitle:@"取消"
                    sureTitle:@"确定"
                 cancelAction:nil
                   sureAction:^{
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[SDImageCache sharedImageCache] clearWithCacheType:(SDImageCacheTypeAll)
                                                     completion:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MBProgressHUD showHUDMsg:@"清理成功"
                                       toView:nil
                              completionBlock:^{
                        self.cacheV.descLa.text = @"0M";
                    }];
                });
            }];
        });
    }];
}


@end
